<?php

require('config.php');


$page = new rootLayoutPage();
//Vérifier que l'utilisateur est connecté
if(isset($_SESSION['id_user'])&&!isTranslating()) {
	redirectFront('index');
}

if(!isset($_SESSION['referer_connexion']))
	$_SESSION['referer_connexion'] = $_SERVER['HTTP_REFERER'];
//echo $_SESSION['referer_connexion'];
$connexionByUrl = modelProvince::getProvinceByUrl('connexion.php');
	if(isset($connexionByUrl)){
		$page->setBaliseTitle($connexionByUrl->title);
		$page->setDescription($connexionByUrl->meta_title);
		$id_menu_left_tree = modelProvince::getProvinceMenuleft($connexionByUrl->language_link)->id_menu_left_tree;
	}else{
		$page->setBaliseTitle('connexion_balise');
		$page->setDescription('connexion_description');
	}
//$page->addJavaScript('jquery.validate.js');
//-- menu Left page--------------------------------//
$menu = new layoutMenuLeft();

$menuPage = new layoutMenuPage();
$menuPage->setTable('connexion');
$menuPage->setIdShow($id_menu_left_tree);
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$header = new layoutHeader();
$header->setIdShow($id_menu_left_tree);
$page->setHeader($header);

$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
$xml = tmxTranslate('connexion.xml', REP_XLAT_TEMPLATE);
$boo = false;
//-- corps de la page --------------------------------------------------------//
if (isset ($_POST['send'])) {
	try {
		$login = $_POST['mail'];
        $pass = $_POST['password'];
        if(!modelUser::existLogin($login))
            throw new Exception($xml['error_1']);
        $user = modelUser::getUtilisateur($login);
       	if($user->etat_user != modelUser::EMAIL_VALIDE){
        	$mail = new flyLayout(REP_MAIL.'inscription.mail.tpl');
				$mail->start();
				$mail->setVariable('mail_utilisateur', $login);
				$mail->setVariable('lien_activation', 'http://'.$_SERVER['HTTP_HOST'].LANGUAGE_FOLDER.'/validation.php?id='.$user->id_user.'&clef='.$user->key_unique);
				$mail->stop();

				$subject = $xml_subject['resend_mail_validation'];

				
	        	
	         	if(!$_SESSION['user_not_validate']){
	        		$arr = array($login => time());
	        		$_SESSION['user_not_validate'] = $arr;
		        		if(STATUS_EMAIL == 0) {
		        			echo $mail;
			        	}elseif(STATUS_EMAIL == 1) {
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}else {
			        		echo $mail;
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}
	        	}
	        	else{
	        		$sss = $_SESSION['user_not_validate'];
	        		//$sss[$login] = time();
	        		$a = $sss[$login];
	        		if($a){
	        			if((time()-$a)>(MAX_MINUTE_RESEND_MAIL_VALIDATION*60)){
	        				$sss[$login] = time();
	        				$_SESSION['user_not_validate'] = $sss;
	        				if(STATUS_EMAIL == 0) {
			        			echo $mail;
				        	}elseif(STATUS_EMAIL == 1) {
				        		monsterMail($email_mail, $subject, $mail);
				        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
				        	}else {
				        		echo $mail;
				        		monsterMail($email_mail, $subject, $mail);
				        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
				        	}       				
	        			}
	        		}
	        		else{
	        			$sss[$login] = time();
	        			$_SESSION['user_not_validate'] = $sss;
	        			if(STATUS_EMAIL == 0) {
		        			echo $mail;
			        	}elseif(STATUS_EMAIL == 1) {
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}else {
			        		echo $mail;
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}
	        		}
	        	}
            modelUser::insertHisto(modelUser::TYPE_HISTO_ERROR, $login, md5($pass));
            $boo = true;
        }else{
        	modelUser::connexion($login, $pass);
        	Persistence::register(PERSIST_MESSAGE, new Message('Bienvenue', Message::INFO));
			if(isset($_SESSION['referer_connexion'])){
	        	if($_SESSION['referer_connexion'] != '' and $_SESSION['referer_connexion'] != @$_SERVER['SCRIPT_URI']) {
				    header('Location: '.$_SESSION['referer_connexion']);
				    unset($_SESSION['referer_connexion']);
				    exit();
		  		}
			}else{redirectFront('index');}

       	}

	} catch (Exception $e) {

		Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message :: ERROR));
	}
}

$tpl_file='connexion.tpl';
$connexion = new flyLayout(REP_TPL . $tpl_file);
$connexion->start();
	if($boo == true) {
		$connexion->showBlock('imposible');
	}
	else{
		$connexion->showBlock('connexion');
	}
$connexion->stop();
$page->setBody($connexion);
$page->display();

?>